package com.jh.mytext2;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;

    private boolean isHide=false;

    private int number=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDialog();

            }
        });
    }

    public void showDialog(){
        final DefaultDialog dialog=new DefaultDialog(this);
        dialog.setClose(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.hideButton();
                dialog.getBtClose().setVisibility(View.INVISIBLE);
                dialog.setTvText1("关闭窗口你就不是傻逼了吗");

            }
        });

        dialog.setTouch2(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_DOWN) {
                    if (!isHide) {
                        if (number==0) {
                            moveUp(dialog.getButton());

                        }else if(number==1){
                            moveDown(dialog.getButton());

                        }

                    }else{
                        if (dialog.getButton().getText().equals("不是")) {
                            dialog.getButton().setText("是的");
                            dialog.getButton1().setText("不是");

                        }else if(dialog.getButton().getText().equals("是的")){
                            dialog.setTvText1("恭喜你，终于认识到自己是傻逼");
                            dialog.hideButton();

                        }

                    }

                }
                return true;
            }
        });

        dialog.setTouch1(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_DOWN) {
                        if (dialog.getButton1().getText().equals("不是")) {
                            dialog.getButton1().setText("是的");
                            dialog.getButton().setText("不是");


                        }else if(dialog.getButton1().getText().equals("是的")){
                            dialog.setTvText1("恭喜你，终于认识到自己是傻逼");
                            dialog.hideButton();

                        }

                    }


                return true;
            }
        });
        dialog.show();
    }

    public void moveUp(Button button){
        ObjectAnimator animator=ObjectAnimator.ofFloat(button,"translationY",0,-150);
        animator.setDuration(10);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                number=1;
            }
        });


    }

    public void moveDown(Button button){
        ObjectAnimator animator=ObjectAnimator.ofFloat(button,"translationY",-150,0);
        animator.setDuration(10);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                isHide=true;
            }
        });
    }
}
